package com.nanuminc.market.common;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nanuminc.market.NMConstants;
import com.nanuminc.market.R;
import com.nanuminc.market.utils.ImageUtil;

public class TimeLineAdapter extends BaseAdapter {
	LayoutInflater mInflater;
	ArrayList<TimeLineList> arSrc;

	public TimeLineAdapter(Context context, ArrayList<TimeLineList> arItem) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		arSrc = arItem;
	}

	public int getCount() {
		return arSrc.size();
	}

	public TimeLineList getItem(int position) {
		return arSrc.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return arSrc.get(position).timeLineType;
	}

	public int getViewTypeCount(int position) {
		if (arSrc.get(position).timeLineType > 5)
			return 2;
		else
			return 1;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		switch (arSrc.get(position).timeLineType) {
			// A,B급 광고 리스트
			case NMConstants.LIST_AB: {
				convertView = mInflater.inflate(R.layout.timelines_ab, parent,	false);
				TextView tokid_txt = (TextView) convertView.findViewById(R.id.userid);
				tokid_txt.setText(arSrc.get(position).listName);
				TextView tokcont_txt = (TextView) convertView.findViewById(R.id.usercontants);
				tokcont_txt.setText(Html.fromHtml(arSrc.get(position).listContants));
	
				ImageView img = (ImageView) convertView.findViewById(R.id.timelinephoto);
				img.setScaleType(ImageView.ScaleType.FIT_XY);
				if (arSrc.get(position).listImg != null) {
					Bitmap imground;
					ImageUtil imageutil = new ImageUtil();
					imground = imageutil.getRoundedCornerBitmap(arSrc.get(position).listImg);
					img.setImageBitmap(imground);
				}
				TextView tokdistance_txt = (TextView) convertView.findViewById(R.id.timelinedistance);
				tokdistance_txt.setText(arSrc.get(position).distance);
				TextView infoAddr = (TextView) convertView.findViewById(R.id.timelineaddress);
				infoAddr.setText(arSrc.get(position).addr);
				break;
			}
			// C급 광고
			case NMConstants.LIST_C: {
				convertView = mInflater
						.inflate(R.layout.timelines_c, parent, false);
	
				TextView tokid_txt = (TextView) convertView.findViewById(R.id.userid);
				tokid_txt.setText(arSrc.get(position).listName);
				TextView tokdistance_txt = (TextView) convertView.findViewById(R.id.timelinedistance);
				tokdistance_txt.setText(arSrc.get(position).distance);
				TextView tokcont_txt = (TextView) convertView.findViewById(R.id.usercontants);
				tokcont_txt.setText(Html.fromHtml(arSrc.get(position).listContants));
				break;
			}
			// NOMAL LIST
			case NMConstants.LIST_NOMAL: {
				convertView = mInflater.inflate(R.layout.nomallist, parent, false);
				TextView listcust = (TextView) convertView.findViewById(R.id.usercontants);
				listcust.setText(arSrc.get(position).listContants);
				break;
			}
			// 후기 LIST
			case NMConstants.POSTSCRIPT_LIST: {
				convertView = mInflater.inflate(R.layout.postscript, parent, false);
				
				TextView tokid_txt = (TextView) convertView.findViewById(R.id.userid);
				tokid_txt.setText(arSrc.get(position).listName);
				
				TextView listcust = (TextView) convertView.findViewById(R.id.usercontants);
				listcust.setText(arSrc.get(position).listContants);
				
				TextView write_time = (TextView) convertView.findViewById(R.id.writetime);
				write_time.setText(arSrc.get(position).distance);
				
				break;
			}
			
		}

		return convertView;
	}
}
